package j.framework.core;

import j.util.New;
import j.util.StringUtils;

import java.io.Serializable;
import java.util.Map;

/**
 * User: jason
 * Date: 14-6-10 下午2:15
 */
public class Function implements Serializable,Comparable<Function> {

    private Map<String,Action> actions = New.map();
    public void putAction(String actionId,Action action){
        actions.put(actionId,action);
    }
    public boolean hasAction(String actionId){
        return actions.containsKey(actionId);
    }

    private String functionId;

    private String functionPid;

    private String name;

    private String url;

    private String icon;

    private double displayOrder;

    private boolean isJsp;

    public String getFunctionId() {
        return functionId;
    }
    public void setFunctionId(String functionId) {
        this.functionId = functionId;
    }

    public String getFunctionPid() {
        return functionPid;
    }
    public void setFunctionPid(String functionPid) {
        this.functionPid = functionPid;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        if(icon!=null){
            Map<String,Object> map = New.map();
            map.put("static.basepath", FApplication.getResourceUrl("/"));
            this.icon = StringUtils.format(icon, map);
        }else{
            this.icon = icon;
        }
    }
    public void setIcon1(String icon) {
        this.icon = icon;
    }

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public double getDisplayOrder() {
        return displayOrder;
    }
    public void setDisplayOrder(double displayOrder) {
        this.displayOrder = displayOrder;
    }

    public boolean isJsp() {
        return isJsp;
    }
    public void setJsp(boolean isJsp) {
        this.isJsp = isJsp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Function function = (Function) o;

        if (functionId != null ? !functionId.equals(function.functionId) : function.functionId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return functionId != null ? functionId.hashCode() : 0;
    }

    @Override
    public int compareTo(Function o) {
        if(o == null){
            return 1;
        }
        if(this.getDisplayOrder()>o.getDisplayOrder()){
            return -1;
        }else if(this.getDisplayOrder()==o.getDisplayOrder()){
            return 0;
        }
        return 1;
    }
}
